package com.erp.app.servlet.web;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.erp.app.admin.systemMenu.beans.SystemMenuGroupVO;
import com.erp.app.admin.systemMenu.ejb.SystemMenuGroupPK;
import com.erp.app.common.ProjectConstants;
import com.erp.app.util.web.WebUtil;
import com.erp.framework.model.delegate.SimpleSearchMaintStyleDelegate;
import com.erp.framework.util.logger.Logger;

public class MenuIconServlet extends HttpServlet {
    final static public String DELEGATE_SESSION_NAME = "SESSION_SM_DELEGATE";
    final static public String ICON_STORE_SESSION_NAME = "ICON_STORE";

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            byte[] menuIcon = getMenuIcon(request);

            OutputStream out = response.getOutputStream();
            response.setContentType("image/gif");
            response.setStatus(response.SC_OK);

            if (menuIcon == null) {                
                Logger.debug(this, "Icon not found at: ");
            } else {
                out.write(menuIcon);
            }

            out.close();
        } catch (Exception e) {
        	Logger.logExceptions(this, e);
        }

        return;
    }

    protected byte[] getMenuIcon(HttpServletRequest request) {

        byte[] menuIcon = null;
        try {
            HttpSession session = request.getSession(false);

            String companyCode = WebUtil.getAppropriateCompanyCode(session, ProjectConstants.SP_SHARE_SYSTEM_MENU);
            String groupId = request.getParameter("group");
            String menuId = request.getParameter("menu");

            SystemMenuGroupPK pk = new SystemMenuGroupPK(companyCode, groupId, menuId);

            HashMap map = new HashMap();
            if (session != null) {
                map = (HashMap) session.getAttribute(ICON_STORE_SESSION_NAME);
            }

            if (map == null) {
                map = new HashMap();
            }

            if (!map.containsKey(pk)) {
                SimpleSearchMaintStyleDelegate dg = getMasterDelegate(session);

                SystemMenuGroupVO menuGroupVO = (SystemMenuGroupVO) dg.findByPrimaryKey(new SystemMenuGroupVO(), pk);

                if (menuGroupVO != null) {
                    menuIcon = menuGroupVO.getMenuIcon();
                }

                if (menuIcon != null) {
                    map.put(pk, menuIcon);
                }

            } else {

                menuIcon = (byte[]) map.get(pk);

            }

            if (map != null) {
                session.setAttribute(ICON_STORE_SESSION_NAME, map);
            } else {
                session.setAttribute(ICON_STORE_SESSION_NAME, new HashMap());
            }

        } catch (Exception e) {
            System.err.println("MenuIconServlet.getMenuIcon(): " + e.getMessage());
            e.printStackTrace();
        }

        return menuIcon;
    }

    protected SimpleSearchMaintStyleDelegate getMasterDelegate(HttpSession session) {
        SimpleSearchMaintStyleDelegate dg = null;
        try {
            if (session.getAttribute(DELEGATE_SESSION_NAME) != null) {
                dg = (SimpleSearchMaintStyleDelegate) session.getAttribute(DELEGATE_SESSION_NAME);
            } else {
                dg = new SimpleSearchMaintStyleDelegate();
                session.setAttribute(DELEGATE_SESSION_NAME, dg);
            }
        } catch (Exception e) {
        	Logger.logExceptions(this, e);
        }
        return dg;
    }

}
